package com.meilai.project.util.excel;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 张驰
 * @date 2021/8/31 21:07
 */
public class BuildingAreaExcelUtil {

    /**
     * 获取文档
     *
     * @param sheetname 表单名
     * @param title     标题栏
     * @param content   内容
     * @return
     */
    public static XSSFWorkbook getWorkbook(String sheetname, Integer[] width, String[] points, String[] title, String[][] content, String [] sum, boolean hasSize) {
        //新建文档实例
        XSSFWorkbook workbook = new XSSFWorkbook();

        //在文档中添加表单
        XSSFSheet sheet = workbook.createSheet(sheetname);

        Map<String, XSSFCellStyle> styles = createStyles(workbook);

        XSSFCellStyle headStyle = styles.get("head");
        XSSFCellStyle cellStyle = styles.get("cell");
        XSSFCellStyle wrapCellStyle = styles.get("wrapCell");

        for (int i = 0; i < width.length; i++) {
            sheet.setColumnWidth(i, width[i] * 256);
        }

        boolean hasPoints = false;
        if(points.length > 0){
            hasPoints = true;
            int i = 0;
            for (; i < title.length; i++) {
                sheet.addMergedRegion(new CellRangeAddress(0,1,i,i));
            }
            for (; i < (title.length+points.length*(hasSize?3:2)); i+=(hasSize?3:2)) {
                sheet.addMergedRegion(new CellRangeAddress(0,0,i,i+(hasSize?2:1)));
            }
            for(int j=content.length+2; j< (content.length+2+sum.length);j++){
                sheet.addMergedRegion(new CellRangeAddress(j,j,0,content[0].length-1));
            }
        }

        //创建第一行，用于填充标题
        XSSFRow titleRow = sheet.createRow(0);
        if(!hasPoints) titleRow.setHeightInPoints(25);
        int titleIndex = 0;
        //填充标题
        for (; titleIndex < title.length; titleIndex++) {
            //创建单元格
            XSSFCell cell = titleRow.createCell(titleIndex);
            //设置单元格内容
            cell.setCellValue(title[titleIndex]);
            //设置单元格样式
            cell.setCellStyle(headStyle);
        }
        for (; titleIndex < (title.length+points.length*(hasSize?3:2)); titleIndex+=(hasSize?3:2)) {
            //创建单元格
            XSSFCell cell = titleRow.createCell(titleIndex);
            //设置单元格内容
            cell.setCellValue(points[(titleIndex-title.length)/(hasSize?3:2)]);
            //设置单元格样式
            cell.setCellStyle(headStyle);

            XSSFCell cell1 = titleRow.createCell(titleIndex+1);
            cell1.setCellStyle(headStyle);

            if(hasSize){
                XSSFCell cell2 = titleRow.createCell(titleIndex+2);
                cell2.setCellStyle(headStyle);
            }
        }


        if(hasPoints){
            XSSFRow titleRow1 = sheet.createRow(1);
            //填充标题
            for (int i = 0; i < (title.length+points.length*(hasSize?3:2)); i++) {
                //创建单元格
                XSSFCell cell = titleRow1.createCell(i);
                //设置单元格样式
                cell.setCellStyle(headStyle);
                if(i>=title.length){
                    if((i-title.length)%(hasSize?3:2) == 0){
                        cell.setCellValue("可选");
                    }
                    if((i-title.length)%(hasSize?3:2) == 1){
                        cell.setCellValue("总数");
                    }
                    if(hasSize && (i-title.length)%(hasSize?3:2) == 2){
                        cell.setCellValue("尺寸");
                    }
                }

            }
        }

        //填充内容
        for (int i = 0; i < content.length; i++) {
            //创建行
            XSSFRow row = sheet.createRow(i + (hasPoints?2:1));
            //遍历某一行
            for (int j = 0; j < content[i].length; j++) {
                //创建单元格
                XSSFCell cell = row.createCell(j);
                //设置单元格内容
                cell.setCellValue(content[i][j]);
                //设置单元格样式
                cell.setCellStyle(cellStyle);
            }
        }

        //填充和积极
        for (int i = 0; i < sum.length; i++) {
            //创建行
            XSSFRow row = sheet.createRow(i + content.length + (hasPoints?2:1));
            //遍历某一行
            for (int j = 0; j < content[i].length; j++) {
                //创建单元格
                XSSFCell cell = row.createCell(j);
                //设置单元格内容
                if(j==0) cell.setCellValue(sum[i]);
                //设置单元格样式
                cell.setCellStyle(cellStyle);
            }
        }

        //返回文档实例
        return workbook;
    }


    private String fmtString(String str) {
        if (StringUtils.isEmpty(str)) {
            return "";
        }
        return str;
    }

    /**
     * 单元格样式
     * @param wb
     * @return
     */
    private static Map<String, XSSFCellStyle> createStyles(XSSFWorkbook wb) {
        Map<String, XSSFCellStyle> styles = new HashMap<String, XSSFCellStyle>();

        Font headFont = wb.createFont();
        headFont.setFontHeightInPoints((short) 12);
        headFont.setColor(IndexedColors.BLACK.getIndex());

        Font cellFont = wb.createFont();
        cellFont.setFontHeightInPoints((short) 10);
        cellFont.setColor(IndexedColors.BLACK.getIndex());

        XSSFCellStyle headCellStyle = createBorderedStyle(wb);
        headCellStyle.setWrapText(true);
        headCellStyle.setAlignment(HorizontalAlignment.CENTER);
        headCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        headCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT
                .getIndex());
        headCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        headCellStyle.setFont(cellFont);
        styles.put("head", headCellStyle);

        XSSFCellStyle headTitleStyle = createBorderedStyle(wb);
        headTitleStyle.setWrapText(true);
        headTitleStyle.setAlignment(HorizontalAlignment.CENTER);
        headTitleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        headTitleStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT
                .getIndex());
        headTitleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        headTitleStyle.setFont(headFont);
        styles.put("headTitle", headTitleStyle);

        XSSFCellStyle cellStyle = createBorderedStyle(wb);
        cellStyle.setWrapText(false);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setFont(cellFont);
        styles.put("cell", cellStyle);

        XSSFCellStyle wrapCellStyle = createBorderedStyle(wb);
        wrapCellStyle.setWrapText(true);
        wrapCellStyle.setAlignment(HorizontalAlignment.CENTER);
        wrapCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        wrapCellStyle.setFont(cellFont);
        styles.put("wrapCell", cellStyle);

        XSSFCellStyle urlStyle = createBorderedStyle(wb);
        Font urlFont = wb.createFont();
        urlFont.setColor(IndexedColors.BLUE.getIndex());
        urlFont.setUnderline(HSSFFont.U_SINGLE);
        urlStyle.setFont(urlFont);
        //cellStyle.setWrapText(true);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        styles.put("urlStyle", urlStyle);

        return styles;
    }

    private static XSSFCellStyle createBorderedStyle(XSSFWorkbook wb) {
        XSSFCellStyle style = wb.createCellStyle();
        style.setBorderRight(BorderStyle.THIN);
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderBottom(BorderStyle.THIN);
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderLeft(BorderStyle.THIN);
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderTop(BorderStyle.THIN);
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());
        return style;
    }
}
